/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.services.io.configuration;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * This class represents the Tree data structure to help XML processing. 
 * While parsing an XML document, the XML DOM is projected into a ConfigurationTreeNode structure.
 */
public class ConfigurationTreeNode {

    private static ConfigurationTreeNode NULL_TREE_NODE;

    private String                          nodeValue;
    private HashMap<String, String>         attributes;
    private List<ConfigurationTreeNode>     children;
    private ConfigurationTreeNode           parent;

    static{
        NULL_TREE_NODE = new ConfigurationTreeNode( null, null, null );
    }

    /**
     * Creates a new instance of ConfigurationTreeNode
     * @param nodeValue value of the node
     * @param parent parent node
     * @param attributes atributes
     */
    public ConfigurationTreeNode(String nodeValue, ConfigurationTreeNode parent, HashMap<String, String> attributes ) {
        this.nodeValue  = nodeValue;
        this.parent     = parent;
        children        = new LinkedList<ConfigurationTreeNode>();
        this.attributes = attributes;
    }

    /**
     * Getter method for nodeValue
     */
    public String getNodeValue() {
        return nodeValue;
    }

    /**
     * Getter method for attributes
     */
    public HashMap getAttributes() {
        return attributes;
    }

    /**
     * Return the existence of a parameter
     * @param attributeName nem of the attribute
     * @return attribute existence
     */
    public boolean hasAttribute( String attributeName ){
        return getAttribute(attributeName) != null;
    }

    /**
     * Return the value of a parameter
     * @param attributeName nem of the attribute
     * @return attribute value
     */
    public String getAttribute( String attributeName ){
        return attributes.get( attributeName );
        /*
        for( int i=0; i< attributes.length; ++i )
            if( attributes[i][0].equals(attributeName) )
                return attributes[i][1];
        return null; */
    }

    /**
     * Getter method to access children nodes
     */
    public List<ConfigurationTreeNode> getChildren() {
        return children;
    }

    /**
     * Getter method for parent node
     */
    public ConfigurationTreeNode getParent() {
        return parent;
    }

    /**
     * Retrieves the child node with the given data
     * @param value data field of the child node
     * @return child node found, null if no node found
     */
    public ConfigurationTreeNode getChild( String value ){
        for (ConfigurationTreeNode configurationTreeNode : children) {
            if( configurationTreeNode.getNodeValue().equals( value ) )
                return configurationTreeNode;
        }
        return null;
    }

    /**
     * Retrieves the child node with the given data
     * @param value data field of the child node
     * @param parameterName name of the parameter of the node
     * @param parameterValue value of the parameter of the node
     * @return child node found, null if no node found
     */
    public ConfigurationTreeNode getChild( String value, String parameterName, String parameterValue ){
        for (ConfigurationTreeNode configurationTreeNode : children) {
            if(
                    configurationTreeNode.getNodeValue().equals( value ) 
                    && configurationTreeNode.hasAttribute(parameterName)
                    && configurationTreeNode.getAttribute(parameterName).equalsIgnoreCase(parameterValue)
            )
                return configurationTreeNode;
        }
        return null;
    }

    /**
     * Tells whether this node is leaf or not
     */
    public boolean isLeaf(){
        return children.size() == 0;
    }

    /**
     * Adds a new child to this node
     * @param childValue data of the child node
     * @param attributes attributes of the child node
     * @return Child node
     */
    public ConfigurationTreeNode addChild( String childValue, HashMap<String, String> attributes ){
        return addChild( new ConfigurationTreeNode( childValue.trim(), this, attributes ) );
    }

    public ConfigurationTreeNode addChild( ConfigurationTreeNode node ){
        children.add( node );
        return node;
    }

    /**
     * Getter method for the first child
     */
    public ConfigurationTreeNode getContainedValue(){
        return children.size() == 0 ? NULL_TREE_NODE : children.get( 0 );
    }

    /**
     * Getter method to access children nodes
     */
    public List<ConfigurationTreeNode> getContainedValues(){
        return children;
    }

    /**
     * String representation of the node containing the data, attributes and children's string too
     */
    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for( String key : attributes.keySet() )
            sb2.append(" " + key + "=" + attributes.get( key ) );
        sb.append( isLeaf() ? nodeValue : "<" + nodeValue + sb2 + ">" );
        for (ConfigurationTreeNode configurationTreeNode : children) {
            sb.append( configurationTreeNode );
        }
        sb.append( isLeaf() ? "" : "</" + nodeValue + ">" );
        return sb.toString();
    }

}
